package com.zb.mvc.controller.base;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.zb.entity.base.JsonCommon;
import com.zb.entity.base.JsonForList;
import com.zb.entity.charger.OmsChargerOfflineWhitelist;
import com.zb.entity.system.OmsOperList;
import com.zb.mvc.model.AddWhiteListModel;
import com.zb.mvc.model.DeleteWhiteListModel;
import com.zb.service.base.OfflineWhiteListService;
import com.zb.util.ActionUtils;
import com.zb.util.OmsConstant;

/**
 * 
 * @author pengyu
 *
 */
@Controller
@RequestMapping("offlineWhitelistApi")
public class ChargerOfflineWhitelistAction extends BaseController
{
	@Autowired
	private OfflineWhiteListService offlineWhitelistServiceImpl;


	@RequestMapping(value = "addList", method = RequestMethod.POST)
	@ResponseBody
	public JsonCommon updateList(AddWhiteListModel model, HttpSession session) throws Exception
	{
		List<String> userCardList = JSON.parseArray(model.getUserCards(), String.class);

		OmsOperList current_user = (OmsOperList) session.getAttribute(BaseAction.CommonParam.SESSION_USER);
		String current_oper_id = current_user.getOper_id();
		offlineWhitelistServiceImpl.addWhiteLists(model.getChargerId(), userCardList, current_oper_id);
		return new JsonCommon<>(OmsConstant.SuccessCodeEnum.SuccessCode.value(), OmsConstant.SuccessCodeEnum.SuccessCode.desc());
	}
	
	@RequestMapping(value="deleteList",method=RequestMethod.POST)
	@ResponseBody
	public JsonCommon<?> deleteOperMerchant(DeleteWhiteListModel model, HttpSession session) throws Exception
	{
		List<String> userCardList = JSON.parseArray(model.getUserCards(), String.class);

		OmsOperList current_user = (OmsOperList) session.getAttribute(BaseAction.CommonParam.SESSION_USER);
		String current_oper_id = current_user.getOper_id();
		offlineWhitelistServiceImpl.deleteWhiteLists(model.getChargerId(), userCardList, current_oper_id);
		return new JsonCommon<>(OmsConstant.SuccessCodeEnum.SuccessCode.value(), OmsConstant.SuccessCodeEnum.SuccessCode.desc());
	}
	
	@RequestMapping(value="findAllOrPage",method=RequestMethod.GET)
	@ResponseBody
	public JsonForList<?> findAllOrPage(HttpServletRequest request, HttpServletResponse response)
	{
		BaseAction b = new BaseAction();
		return offlineWhitelistServiceImpl.findPageObjs(b.buildQueryParam(request, true), this.getObjFromRequest(request,false,true));
	}
	
	
	private OmsChargerOfflineWhitelist getObjFromRequest(HttpServletRequest request,boolean isCreate,boolean isFind)
	{
		OmsOperList current_user =  (OmsOperList) request.getSession().getAttribute(BaseAction.CommonParam.SESSION_USER);
		String loginUserId = current_user.getOper_id();
		return ActionUtils.getParamInstance(OmsChargerOfflineWhitelist.class, ActionUtils.getParameterMap(request),loginUserId,isCreate,isFind);
	}
}
